package com.audaque.springboot.foshanupload.web.webdemo.controller;

import com.audaque.springboot.foshanupload.web.webdemo.model.domain.*;
import com.audaque.springboot.foshanupload.web.webdemo.service.inface.IGreeting;
import com.caucho.hessian.client.HessianProxyFactory;
import com.caucho.hessian.io.HessianInput;
import com.caucho.hessian.io.HessianOutput;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("hessian")
public class HessianController {
    @Autowired
    private ServletWebServerApplicationContext servletWebServerApplicationContext;


    private static Student s = new Student();

    static {
        Employee employee = new Employee();
        employee.setId(1);
        employee.setName("1");
        OuterClass outerClass = new OuterClass();
        outerClass.setOuterVar(1);
        employee.setOuterClass(outerClass);
        s.setName("5wimming");
        s.setSex(2);
        Monitor monitor = null;
        try {
            monitor = new Monitor();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        monitor.setEmployee(employee);
        s.setMyObject(monitor);
        s.setMonitor(monitor);
        s.setEmployee(employee);

        Person person = new Person();
        person.setDescribe("1");
        person.setId("1");
        person.setName("1");
        person.setAge(1);
        person.setHeight(1);
        Country country = new Country();
        country.setId("1");
        country.setName("1");
        person.setPersonCountry(country);


        Person person2 = new Person();
        person2.setDescribe("2");
        person2.setId("2");
        person2.setName("2");
        person2.setAge(2);
        person2.setHeight(2);
        Country country2 = new Country();
        country2.setId("2");
        country2.setName("2");
        person2.setPersonCountry(country2);

        List<Person> personList = new ArrayList<>();
        personList.add(person);
        personList.add(person2);

        s.setPersonList(personList);


    }

    @ResponseBody
    @RequestMapping("serialize")
    public byte[] serialize() throws IOException {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        //Hessian的序列化输出
        HessianOutput ho = new HessianOutput(os);
        ho.writeObject(s);
        byte[] b = os.toByteArray();
        return b;
    }


    @ResponseBody
    @RequestMapping("parse")
    public Object parse() throws IOException, ClassNotFoundException {
        ByteArrayInputStream is = new ByteArrayInputStream(serialize());
        //Hessian的反序列化读取对象
        HessianInput hi = new HessianInput(is);
        return hi.readObject();
    }



    @ResponseBody
    @RequestMapping("proxy")
    public Person proxy() throws IOException {
        InetAddress localHost = InetAddress.getLocalHost();
        String hostAddress = localHost.getHostAddress();
        int port = servletWebServerApplicationContext.getWebServer().getPort();
        String url = "http://"+hostAddress+":" + port + "/webdemo/hessian";

        HessianProxyFactory factory = new HessianProxyFactory();
        IGreeting greeting = (IGreeting) factory.create(IGreeting.class, url);
        Person person = new Person();
        person.setDescribe("1");
        person.setId("1");
        person.setName("1");
        person.setAge(1);
        //底层是一个post请求
        return greeting.sayHello(person);
    }


}
